package info

import (
	"github.com/shirou/gopsutil/disk"
	"log"
)

type DiskInfos struct {
	Disks []DiskInfo `json:"disks"`
}

type DiskInfo struct {
	Name  string `json:"name"`
	Total uint64 `json:"total"`
	Used  uint64 `json:"used"`
	Free  uint64 `json:"free"`
}

// disk info
func GetDiskInfo() (interface{}, error) {
	parts, err := disk.Partitions(true)
	if err != nil {
		log.Printf("get Partitions failed, err:%v\n", err)
		return nil, err
	}

	disksdata := DiskInfos{}
	//log.Println(parts)

	for _, part := range parts {
		//fmt.Printf("part:%v\n", part.String())
		diskInfo, _ := disk.Usage(part.Mountpoint)
		if diskInfo.Total == 0 {
			continue
		}
		diskdata := DiskInfo{}
		diskdata.Name = part.Mountpoint
		diskdata.Total = diskInfo.Total
		diskdata.Used = diskInfo.Used
		diskdata.Free = diskInfo.Free
		//fmt.Printf("disk info:used:%v free:%v\n", diskInfo.UsedPercent, diskInfo.Free)
		//diskdata[index] = diskInfo.String()

		disksdata.Disks = append(disksdata.Disks, diskdata)
	}

	//ioStat, _ := disk.IOCounters()
	//for k, v := range ioStat {
	//  fmt.Printf("%v:%v\n", k, v)
	//}

	return disksdata, nil
}

// 只能用于windows的磁盘读取
//func GetDiskInfo2()(interface{}, error) {
//  var storageinfo []storageInfo
//  var loaclStorages []Storage
//  err := wmi.Query("Select * from Win32_LogicalDisk", &storageinfo)
//  if err != nil {
//    return nil, err
//  }
//
//  for _, storage := range storageinfo {
//    info := Storage{
//      Name:       storage.Name,
//      FileSystem: storage.FileSystem,
//      Total:      storage.Size,
//      Free:       storage.FreeSpace,
//    }
//    loaclStorages = append(loaclStorages, info)
//  }
//
//  return loaclStorages, nil
//}
